/*
 * sparse_band.cpp
 *
 *  Created on: Jun 22, 2013
 *      Author: tifuera
 */

#include <stdio.h>

#include "sparse_band.h"

int get_band_num_elements(int order, int half_bandwith)
{
	int lower_num = ((1 + half_bandwith) * half_bandwith) / 2
			+ (order - (half_bandwith + 1)) * half_bandwith;

	return lower_num * 2 + order;
}

//TODO test this method
int get_index(int i, int j, int half_bandwith, int order)
{
	int res = 0;
	int max_top = half_bandwith;
	int low_bot = order - 1 - (half_bandwith - 1);

	if (i <= (half_bandwith - 1))
	{
		max_top = i;
	}
	res += ((half_bandwith + 1 + (half_bandwith + 1 + max_top - 1)) * max_top)
			/ 2;

	if (i <= half_bandwith)
	{
		return res + j;
	}

	if (i > low_bot)
	{
		res += (order - 2 * half_bandwith) * (2 * half_bandwith + 1);
		res += ((2 * half_bandwith + (half_bandwith + order - (i - 1)))
				* (i - low_bot)) / 2;
	}
	else
	{
		res += (i - (half_bandwith - 1) - 1) * (2 * half_bandwith + 1);
	}

	return j - (i - half_bandwith) + res;

}

void print_band_matrix(int order, int half_bandwith, double * matrix)
{
	if(order > 20)
	{

		return;
	}
	int i, j;
	printf("<<<<<<<<<<<<<< Printing matrix >>>>>>>>>>>>>>>\n");
	for (i = 0; i < order; i++)
	{
		for (j = 0; j < order; j++)
		{
			if ((i - half_bandwith <= j) && (i + half_bandwith >= j))
			{
				printf(" %6.1f", matrix[get_index(i, j, half_bandwith, order)]);
			}
			else
			{
				printf(" %6.1f", 0.);
			}
		}
		printf("\n");
	}
}

